package com.yang.web;

import com.hmaill.common.dto.PageDTO;
import com.yang.dto.ItemDto;
import com.yang.dto.RequestParams;
import com.yang.service.TbItemService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author lo
 */
@RequestMapping("search")
@RestController
public class TbItemController {

    @Autowired
    private TbItemService service;

    /**
     * 自动补全功能
     * @param key
     * @return
     */
    @GetMapping("suggestion")
    public List<String> getSuggestions(@RequestParam("key") String key) {
        return service.getSuggestions(key);
    }

    /**
     * 桶聚合查询
     * @param requestParams
     * @return
     */
    @PostMapping("filters")
    public Map<String, List<String>> buckets(@RequestBody RequestParams requestParams) {
        return service.buckets(requestParams);
    }

    /**
     * 查询所有数据
     * @param requestParams
     * @return
     */
    @PostMapping("list")
    public PageDTO<ItemDto> getList(@RequestBody RequestParams requestParams) {
        return service.getItem(requestParams);
    }

    /**
     * 添加修改数据
     * 添加和修改的代码是一样的
     * 队列topic.queue2绑定交换机item.topic
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            key = "#.upData"
    ))
    public void listenTopicQueue1(Long id){
        service.upDataItemDoc(id);
    }

    /**
     * 删除数据
     * 队列topic.queue2绑定交换机item.topic
     * @param id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "item.topic", type = ExchangeTypes.TOPIC),
            key = "#.deleteData"
    ))
    public void listenTopicQueue2(Long id){
        service.deleteDataItem(id);
    }
}
